package com.hzdesk.media.koreabatchmatvector.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.MouseUserDevicesinfoBean;
import com.hzdesk.media.koreabatchmatvector.bean.UserBean;
import com.hzdesk.media.koreabatchmatvector.inter.ColorChangeClick;


/**
 * Created by Administrator on 2018/4/25.
 */

public class ColorPopWindow extends PopupWindow implements View.OnClickListener{
    private View contentView;
    private RelativeLayout black_re,red_re,orange_re,yellow_re,green_re,sky_blue_re,blue_re,purple_re;
    private ImageView imageView_black,imageView_red,imageView_orange,imageView_yellow,imageView_green,imageView_sky_blue,imageView_blue,imageView_purple;
    private ImageView imageView_black_s,imageView_red_s,imageView_orange_s,imageView_yellow_s,imageView_green_s,imageView_sky_blue_s,imageView_blue_s,imageView_purple_s;
    private ColorChangeClick colorChangeClick;
    private Activity context;
    public void setColorChangeClickListener(ColorChangeClick colorChangeClick1){this.colorChangeClick=colorChangeClick1;}

    public ColorPopWindow(Activity context) {
        super(context);
        this.context=context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView=inflater.inflate(R.layout.popup_color,null);
        imageView_black=(ImageView)contentView.findViewById(R.id.color_popup_black);
        imageView_red=(ImageView)contentView.findViewById(R.id.color_popup_red);
        imageView_orange=(ImageView)contentView.findViewById(R.id.color_popup_orange);
        imageView_yellow=(ImageView)contentView.findViewById(R.id.color_popup_yellow);
        imageView_green=(ImageView)contentView.findViewById(R.id.color_popup_green);
        imageView_sky_blue=(ImageView)contentView.findViewById(R.id.color_popup_sky_blue);
        imageView_blue=(ImageView)contentView.findViewById(R.id.color_popup_blue);
        imageView_purple=(ImageView)contentView.findViewById(R.id.color_popup_purple);


        imageView_black_s=(ImageView)contentView.findViewById(R.id.color_popup_black_s);
        imageView_red_s=(ImageView)contentView.findViewById(R.id.color_popup_red_s);
        imageView_orange_s=(ImageView)contentView.findViewById(R.id.color_popup_orange_s);
        imageView_yellow_s=(ImageView)contentView.findViewById(R.id.color_popup_yellow_s);
        imageView_green_s=(ImageView)contentView.findViewById(R.id.color_popup_green_s);
        imageView_sky_blue_s=(ImageView)contentView.findViewById(R.id.color_popup_sky_blue_s);
        imageView_blue_s=(ImageView)contentView.findViewById(R.id.color_popup_blue_s);
        imageView_purple_s=(ImageView)contentView.findViewById(R.id.color_popup_purple_s);

        black_re=contentView.findViewById(R.id.color_popup_black_re);
        red_re=contentView.findViewById(R.id.color_popup_red_re);
        orange_re=contentView.findViewById(R.id.color_popup_orange_re);
        yellow_re=contentView.findViewById(R.id.color_popup_yellow_re);
        green_re=contentView.findViewById(R.id.color_popup_green_re);
        sky_blue_re=contentView.findViewById(R.id.color_popup_sky_blue_re);
        blue_re=contentView.findViewById(R.id.color_popup_blue_re);
        purple_re=contentView.findViewById(R.id.color_popup_purple_re);

        black_re.setOnClickListener(this);
        red_re.setOnClickListener(this);
        orange_re.setOnClickListener(this);
        yellow_re.setOnClickListener(this);
        green_re.setOnClickListener(this);
        sky_blue_re.setOnClickListener(this);
        blue_re.setOnClickListener(this);
        purple_re.setOnClickListener(this);

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(contentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
       // this.setWidth(h/5);
        // 设置SelectPicPopupWindow弹出窗体的高
        //this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.AnimationPreview);
    }
    public void showPopupWindow(LinearLayout linearLayout){
        if (!this.isShowing()) {
            this.showAsDropDown(linearLayout, linearLayout.getLayoutParams().width / 2, 18);
            //imageView.setImageBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.main_color_s));
            changeUser();
        } else {
            //imageView.setImageBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.main_color));
            this.dismiss();
        }
    }

    public void changeUser(){
        if (MyClientApplication.getUserNum()>0){
            MouseUserDevicesinfoBean bean=MyClientApplication.getMouseUserDeviceinfoBean(context,MyClientApplication.getUserNum());
            //UserBean userBean=MyClientApplication.getUserBeanList().get(MyClientApplication.getUserNum()-1);
            resetImage();
            changeImage(bean.getColor());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.color_popup_black_re:
                colorChangeClick.click("000000");
                resetImage();
                imageView_black_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_red_re:
                colorChangeClick.click("ff0000");
                resetImage();
                imageView_red_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_orange_re:
                colorChangeClick.click("ff9600");
                resetImage();
                imageView_orange_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_yellow_re:
                colorChangeClick.click("ffff00");
                resetImage();
                imageView_yellow_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_green_re:
                colorChangeClick.click("00ff00");
                resetImage();
                imageView_green_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_sky_blue_re:
                colorChangeClick.click("87ceeb");
                resetImage();
                imageView_sky_blue_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_blue_re:
                colorChangeClick.click("0000ff");
                resetImage();
                imageView_blue_s.setVisibility(View.VISIBLE);
                break;
            case R.id.color_popup_purple_re:
                colorChangeClick.click("ff00ff");
                resetImage();
                imageView_purple_s.setVisibility(View.VISIBLE);
                break;
        }

    }

    private void changeImage(String color){
        switch (color){
            case "ff0000":
                imageView_red_s.setVisibility(View.VISIBLE);
                break;
            case "ff9600":
                imageView_orange_s.setVisibility(View.VISIBLE);
                break;
            case "ffff00":
                imageView_yellow_s.setVisibility(View.VISIBLE);
                break;
            case "00ff00":
                imageView_green_s.setVisibility(View.VISIBLE);
                break;
            case "87ceeb":
                imageView_sky_blue_s.setVisibility(View.VISIBLE);
                break;
            case "0000ff":
                imageView_blue_s.setVisibility(View.VISIBLE);
                break;
            case "ff00ff":
                imageView_purple_s.setVisibility(View.VISIBLE);
                break;
            case "000000":
                imageView_black_s.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

    }

    private void resetImage(){
        imageView_black_s.setVisibility(View.GONE);
        imageView_red_s.setVisibility(View.GONE);
        imageView_orange_s.setVisibility(View.GONE);
        imageView_yellow_s.setVisibility(View.GONE);
        imageView_green_s.setVisibility(View.GONE);
        imageView_sky_blue_s.setVisibility(View.GONE);
        imageView_blue_s.setVisibility(View.GONE);
        imageView_purple_s.setVisibility(View.GONE);
    }



}
